/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import db.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 韩艺波
 */
public class OrderInfoDaoImpl implements OrderInfoDao{
    public static void main(String[] args){
        List<OrderInfo> list=new OrderInfoDaoImpl().displayAllOrderInfo();
        System.out.println("KK");
    }

    @Override
    public List<OrderInfo> displayAllOrderInfo() {
        Connection conn=null;
        Statement stat=null;
        ResultSet rs=null;
        String sql="select * from OrderInfo";
        List<OrderInfo> list=new ArrayList<OrderInfo>();
        try{
            conn=JDBCUtil.getConnection();
            stat=conn.createStatement();
            rs=stat.executeQuery(sql);
            while(rs.next()){
                OrderInfo order=new OrderInfo();
                order.setCID(rs.getInt("CID"));
                order.setOID(rs.getInt("OID"));
                order.setPID(rs.getInt("PID"));
                order.setOrderNumber(rs.getInt("OrderNumber"));
                order.setOrderTime(rs.getString("OrderTime"));
                order.setOrderDesc(rs.getString("OrderDesc"));
                list.add(order);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, stat, conn);
        }
        return list;
    }

    @Override
    public int updateOrderInfo(OrderInfo order) {
        String sql="UPDATE orderinfo SET  PID=?, CID=?, OrderNumber=?,OrderTime=?,OrderDesc=? where OID=?";
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        
        int count=0;
        try{
            conn=JDBCUtil.getConnection();
            pstat=conn.prepareCall(sql);
            pstat.setInt(1,order.getPID());
            pstat.setInt(2, order.getCID());
            pstat.setInt(3, order.getOrderNumber());
            pstat.setString(4, order.getOrderTime());
            pstat.setString(5, order.getOrderDesc());
            pstat.setInt(6,order.getOID());
            count=pstat.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, pstat, conn);
        }
        return count;
    }

    @Override
    public int inputOrderInfo(OrderInfo order) {
       
        String sql="insert  into orderinfo(OID,PID,CID,OrderNumber,OrderDesc) values(?,?,?,?,?)";
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        
        int count=0;
       try{
            conn=JDBCUtil.getConnection();
            pstat=conn.prepareCall(sql);
            pstat.setInt(1, order.getOID());
            pstat.setInt(2, order.getPID());
            pstat.setInt(3, order.getCID());
            pstat.setInt(4, order.getOrderNumber());
            //pstat.setString(5, order.getOrderTime());
            pstat.setString(5, order.getOrderDesc());
            count=pstat.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, pstat, conn);
        }
        return count;
    
    }
    
}
